﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;
using System.Web;
using System.Runtime.CompilerServices;
using System.Collections;
using System.Text.RegularExpressions;

namespace LakeSide.Core.Caching
{
    /// <summary>
    /// 缓存类，实现接口ICacheManager
    /// </summary>
    public class CacheManager : ICacheManager
    {
        /// <summary>
        /// 添加缓存对象
        /// </summary>
        /// <param name="Name">名称</param>
        /// <param name="Obj">object对象</param>
        /// <param name="TimeOut">过期时间</param>
        /// <param name="Priority">优先级</param>
        public void Insert(string Name, object Obj, double TimeOut, CacheItemPriority Priority)
        {
            Insert(Name, Obj, null, TimeOut, TimeSpan.Zero, Priority, null);
        }

        /// <summary>
        /// 添加缓存对象
        /// </summary>
        /// <param name="Key">键/名称</param>
        /// <param name="Obj">object对象</param>
        /// <param name="Dependency">依赖对象CacheDependency</param>
        /// <param name="TimeOut">过期时间</param>
        /// <param name="SlidingExpiration">时间间隔</param>
        /// <param name="Priority">优先级</param>
        /// <param name="RemovedCallback">移除缓存后的调用委托</param>
        public void Insert(string Key, object Obj, CacheDependency Dependency, double TimeOut, TimeSpan SlidingExpiration, CacheItemPriority Priority, CacheItemRemovedCallback RemovedCallback)
        {
            if ((Obj != null))
            {
                Cache Cache = HttpRuntime.Cache;
                if (Cache[Key] == null)
                {
                    Cache.Insert(Key, RuntimeHelpers.GetObjectValue(Obj), Dependency, DateTime.Now.AddSeconds(TimeOut), SlidingExpiration, Priority, RemovedCallback);
                }
            }
        }

        /// <summary>
        /// 得到缓存值
        /// </summary>
        /// <param name="Key">键</param>
        /// <returns></returns>
        public object Get(string Key)
        {
            Cache Cache = HttpRuntime.Cache;
            return Cache[Key];
        }
        /// <summary>
        /// 移除缓存项
        /// </summary>
        /// <param name="Key">键</param>
        public void Remove(string Key)
        {
            Cache Cache = HttpRuntime.Cache;
            if (Cache[Key] != null)
            {
                Cache.Remove(Key);
            }
        }

        /// <summary>
        /// 根据正则表达式移除缓存
        /// </summary>
        /// <param name="pattern">正则字符串</param>
        public void RemoveByPattern(string pattern)
        {
            Cache Cache = HttpRuntime.Cache;
            IDictionaryEnumerator enumerator = Cache.GetEnumerator();
            Regex rgx = new Regex(pattern, (RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase)));
            while (enumerator.MoveNext())
            {
                if (rgx.IsMatch(enumerator.Key.ToString()))
                {
                    Cache.Remove(enumerator.Key.ToString());
                }
            }
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        public void Clear()
        {
            Cache Cache = HttpRuntime.Cache;
            IDictionaryEnumerator enumerator = Cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Cache.Remove(enumerator.Key.ToString());
            }
        }

        /// <summary>
        /// 得到缓存项的优先级字典
        /// </summary>
        /// <returns></returns>
        public Dictionary<byte, string> CacheItemPriorities()
        {
            Dictionary<byte, string> dic = new Dictionary<byte, string>();
            dic.Add((byte)CacheItemPriority.Low, CacheItemPriority.Low.ToString());
            dic.Add((byte)CacheItemPriority.BelowNormal, CacheItemPriority.BelowNormal.ToString());
            dic.Add((byte)CacheItemPriority.Normal, CacheItemPriority.Normal.ToString());
            dic.Add((byte)CacheItemPriority.AboveNormal, CacheItemPriority.AboveNormal.ToString());
            dic.Add((byte)CacheItemPriority.High, CacheItemPriority.High.ToString());
            dic.Add((byte)CacheItemPriority.NotRemovable, CacheItemPriority.NotRemovable.ToString());
            return dic;
        }
    }
}
